package com.atghost.miniofile.controller;

import com.alibaba.fastjson.JSONObject;
import com.atghost.miniofile.dto.req.FileBo;
import com.atghost.miniofile.dto.res.CommResp;
import com.atghost.miniofile.dto.res.FileResp;
import com.atghost.miniofile.util.FileBlh;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Admin
 * @version 1.0
 * @date 2024-5-19 14:45
 **/
@RestController
@RequestMapping("/api/file")
public class FileController {

    private static Log logger = LogFactory.getLog(FileController.class);

    @Resource
    private FileBlh fileBlh;

    /*********************************前端页面的视图******************************************/
    @RequestMapping(value = "/upload")
    public ModelAndView homeUpload() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("my-upload");
        return modelAndView;
    }

    @RequestMapping(value = "/video")
    public ModelAndView homeVideo(String url) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("my-video");
        modelAndView.addObject("url",url);
        return modelAndView;
    }


    /***********************************文件上传 接口访问****************************************/

    /**
     * step1 校验文件是否存在, 入参:md5
     */
    @GetMapping(value = "checkSplitFile")
    public FileResp checkSplitFileRest(String md5) {
        FileResp resp = new FileResp();
        FileBo bo = new FileBo();
        bo.setMd5(md5);
        fileBlh.checkSplitFile(bo);
        resp.setFileUrl(bo.getFileUrl());
        resp.setCode(bo.getCode());
        CommResp.setResp(resp, bo);
        resp.setResp();
        return resp;
    }

    /**
     * step 2 切片上传 分片上传文件, 入参:data
     */
    @PostMapping(value = "uploadSplitFile")
    public FileResp uploadSplitFileRest(HttpServletRequest req) {
        FileResp resp = new FileResp();
        FileBo bo = new FileBo();
        bo.setReq(req);
        fileBlh.uploadSplitFile(bo);
        resp.setCode(bo.getCode());
        CommResp.setResp(resp, bo);
        resp.setResp();
        return resp;
    }

    /**
     * step 3 文件合并, 入参:shardCount/fileName/md5/fileType/fileSize
     */
    @GetMapping(value = "mergeSplitFile")
    public FileResp mergeSplitFileRest(HttpServletRequest req) {
        FileResp resp = new FileResp();
        FileBo bo = new FileBo();
        bo.setShardCount(Integer.valueOf(req.getParameter("shardCount")));
        bo.setFileName(req.getParameter("fileName"));
        bo.setMd5(req.getParameter("md5"));
        bo.setFileType(req.getParameter("fileType"));
        bo.setFileSize(Long.valueOf(req.getParameter("fileSize")));
        fileBlh.mergeSplitFile(bo);
        resp.setCode(bo.getCode());
        resp.setFileName(bo.getFileName());
        CommResp.setResp(resp, bo);
        resp.setResp();
        return resp;
    }

    /********************************直接上传:不分片***************************/
    /**
     * 直接上传文件, 入参:file, 出参:fileName
     */
    @PostMapping(value = "uploadFile")
    public FileResp uploadFileRest(HttpServletRequest req) {
        FileResp resp = new FileResp();
        FileBo bo = new FileBo();
        bo.setReq(req);
        fileBlh.uploadFile(bo);
        resp.setFileName(bo.getFileName());
        CommResp.setResp(resp, bo);
        resp.setResp();
        return resp;
    }
    /********************************直接下载***************************/
    /**
     * @description 获取文件下载Url, 入参:fileName, 出参:fileUrl
     */
    @PostMapping(value = "getFileUrl", consumes = "application/json")
    public FileResp getFileUrlRest(@RequestBody JSONObject req) {
        FileResp resp = new FileResp();
        FileBo bo = new FileBo();
        bo.setFileName(req.getString("fileName"));
        fileBlh.getFileUrl(bo);
        resp.setFileUrl(bo.getFileUrl());
        CommResp.setResp(resp, bo);
        resp.setResp();
        return resp;
    }
    /********************************删除文件***************************/
    /**
     * @description 删除文件, 入参:fileName
     */
    @PostMapping(value = "deleteFile", consumes = "application/json")
    public FileResp deleteFileRest(@RequestBody JSONObject req) {
        FileResp resp = new FileResp();
        FileBo bo = new FileBo();
        bo.setFileName(req.getString("fileName"));
        fileBlh.deleteFile(bo);
        CommResp.setResp(resp, bo);
        resp.setResp();
        return resp;
    }

    /********************************分片下载***************************/
    /**
     * @description 获取文件分片下载信息, 入参:fileName, 出参:shardCount
     */
    @PostMapping(value = "getSplitFileInfo", consumes = "application/json")
    public FileResp getSplitFileInfoRest(@RequestBody JSONObject req) {
        FileResp resp = new FileResp();
        FileBo bo = new FileBo();
        bo.setFileName(req.getString("fileName"));
        fileBlh.getSplitFileInfo(bo);
        resp.setShardCount(bo.getShardCount());
        CommResp.setResp(resp, bo);
        resp.setResp();
        return resp;
    }

    /**
     * @description 文件分片下载, 入参:fileName/fileNo, 出参:文件流
     */
    @GetMapping(value = "downSplitFile")
    public void downSplitFileRest(HttpServletRequest req, HttpServletResponse res) {
        FileBo bo = new FileBo();
        bo.setFileName(req.getParameter("fileName"));
        bo.setShardCount(Integer.valueOf(req.getParameter("fileNo")));
        bo.setRes(res);
        fileBlh.downSplitFile(bo);
    }

    /**
     * @description 视频播放
     */
    @GetMapping(value = "videoPlay")
    public void videoPlayRest(HttpServletRequest req, HttpServletResponse res) {
        FileBo bo = new FileBo();
        bo.setReq(req);
        bo.setRes(res);
        bo.setFileName(req.getParameter("video"));
        fileBlh.videoPlay(bo);
    }

}
